<?php
/**
 * 微仓类
 * @author 志杨
 *
 */
class MicroStock
{
	var $db;
	
	function __construct()
	{
		global $db;
		
		$this -> db     = & $db;
	}
	
	/**
	 * 获取某用户微仓商品信息的列表
	 * @param string $field
	 * @param string $cond
	 * @param string $orderRaw
	 * @param number $limitStart
	 * @param number $limitPageSize
	 */
	public function getMemberStockProductsList($field,$cond="",$orderRaw="",$limitStart=0,$limitPageSize=20) {
	    if(empty($field)){
	        $field = "*";
	    }
	    if(!empty($orderRaw)){
	        $orderRaw = 'order by '.$orderRaw;
	    }
	    $sql = "select {$field} from ".MICROSTOCK." as a left join ".SETMEAL." as b on a.sku_id=b.id join haiou_stock_shops as c on a.stock_shop=c.id where a.`status`>0 {$cond} {$orderRaw} limit {$limitStart},{$limitPageSize}";
	    $this->db->query($sql);
	    $result = $this->db->getRows();
	    
	    return $result;
	}
	
	/**
	 * 获取某用户微仓商品信息的总数
	 * @param string $cond
	 */
	public function getMemberStockProductsTotalNum($cond="") {
	    $total = 0;
	    $sql = "select count(*) as total from ".MICROSTOCK." a where `status`>0 {$cond}";
	    $this->db->query($sql);
	    $total = $this->db->fetchField('total');
	     
	    return $total;
	}
	
	/**
	 * 获取所有用户微仓信息的列表
	 * @param string $field
	 * @param string $cond
	 * @param string $orderRaw
	 * @param number $limitStart
	 * @param number $limitPageSize
	 */
	public function getMsList($field,$cond="",$orderRaw="",$limitStart=0,$limitPageSize=20) {
	    if(empty($field)){
	        $field = "*";
	    }
	    if(!empty($orderRaw)){
	        $orderRaw = 'order by '.$orderRaw;
	    }
	    $sql = "select {$field} from ".MICROSTOCK." as a left join ".MEMBER." as b on a.member_id=b.userid where a.`status`>0 {$cond} group by a.member_id {$orderRaw} limit {$limitStart},{$limitPageSize}";
	    $this->db->query($sql);
	    $result = $this->db->getRows();
	    
	    return $result;
	}
	
	/**
	 * 获取所有用户有微仓信息的总数
	 * @param string $cond
	 */
	public function getMsTotalNum($cond="") {
	    $total = 0;
	    $sql = "select count(distinct(member_id)) as total from ".MICROSTOCK." as a where `status`>0 {$cond}";
	    $this->db->query($sql);
	    $total = $this->db->fetchField('total');
	    
	    return $total;
	}
	
	/**
	 * 获取某用户入仓记录
	 * @param string $field
	 * @param string $cond
	 * @param string $orderRaw
	 * @param number $limitStart
	 * @param number $limitPageSize
	 */
	public function getMemberPutInStockRecords($field,$cond="",$orderRaw="",$limitStart=0,$limitPageSize=20) {
	    if(empty($field)){
	        $field = "*";
	    }
	    if(!empty($orderRaw)){
	        $orderRaw = 'order by '.$orderRaw;
	    }
	    $sql = "select {$field} from ".MICROSTOCKORDERPRO." AS a left join ".MICROSTOCKORDER." AS b on a.order_id=b.order_id left join ".MICROSTOCK." AS c on a.sku_id=c.sku_id and c.stock_shop=b.stock_shop join haiou_stock_shops as d on b.stock_shop=d.id where b.status>=2 {$cond} group by a.sku_id,a.order_id {$orderRaw} limit {$limitStart},{$limitPageSize}";
	    $this->db->query($sql);
	    $result = $this->db->getRows();
	    
	    return $result;
	}
	
	/**
	 * 获取某用户入仓记录总数
	 * @param string $cond
	 */
	public function getMemberPutInStockRecordsTotalNum($cond="") {
	    $total = 0;
	    if($_GET['product_id']){
    	    $sql = "select count(*) as total from ".MICROSTOCKORDERPRO." AS a left join ".MICROSTOCKORDER." AS b on a.order_id=b.order_id left join ".MICROSTOCK." AS c on a.sku_id=c.sku_id where b.status>=2 {$cond} ";
	    }else{
	        $sql = "select count(*) as total from ".MICROSTOCKORDERPRO." AS a left join ".MICROSTOCKORDER." AS b on a.order_id=b.order_id where b.status>=2 {$cond} ";
	    }
	    $this->db->query($sql);
	    $total = $this->db->fetchField('total');
	    
	    return $total;
	}
	
	/**
	 * 获取某用户入仓记录总数(按仓库来统计)
	 * @param string $cond
	 */
	public function getMemberPutInStockRecordsTotalNumByShop($cond="") {
	    $res = array();
	    $sql = "select count(*) as total,b.`stock_shop`,d.`title` as shop_name from ".MICROSTOCKORDERPRO." AS a left join ".MICROSTOCKORDER." AS b on a.order_id=b.order_id left join ".MICROSTOCK." AS c on a.sku_id=c.sku_id join haiou_stock_shops as d on b.stock_shop=d.id where b.status>=2 {$cond} group by b.stock_shop";
	    $this->db->query($sql);
	    $res = $this->db->getRows();
	     
	    return $res;
	}
	
	/**
	 * 获取某用户出仓记录
	 * @param unknown $field
	 * @param string $cond
	 * @param string $orderRaw
	 * @param number $limitStart
	 * @param number $limitPageSize
	 */
	public function getMemberOutOfStockRecords($field,$cond="",$orderRaw="",$limitStart=0,$limitPageSize=20) {
	    if(empty($field)){
	        $field = "*";
	    }
	    if(!empty($orderRaw)){
	        $orderRaw = 'order by '.$orderRaw;
	    }
	    $sql = "select {$field} from ".MICROSTOCKOUT." AS a left join ".ORPRO." as b on a.order_id=b.order_id and a.sku_id=b.product_id join haiou_stock_shops as d on a.stock_shop=d.id where 1 {$cond} GROUP BY a.sku_id,a.order_id {$orderRaw} limit {$limitStart},{$limitPageSize}";
	    $this->db->query($sql);
	    $result = $this->db->getRows();
	     
	    return $result;
	}
	
	/**
	 * 获取某用户出仓记录总数
	 * @param string $cond
	 */
	public function getMemberOutOfStockRecordsTotalNum($cond="") {
	    $total = 0;
	    $sql = "select count(*) as total from ".MICROSTOCKOUT." AS a where 1 {$cond} ";
	    $this->db->query($sql);
	    $total = $this->db->fetchField('total');
	     
	    return $total;
	}
	
	/**
	 * 获取某用户出仓记录总数(按仓库来统计)
	 * @param string $cond
	 */
	public function getMemberOutOfStockRecordsTotalNumByShop($cond="") {
	    $res = array();
	    $sql = "select count(*) as total,a.`stock_shop`,c.`title` as shop_name from ".MICROSTOCKOUT." AS a join haiou_stock_shops as c on a.stock_shop=c.id where 1 {$cond} group by a.stock_shop";
	    $this->db->query($sql);
	    $res = $this->db->getRows();
	
	    return $res;
	}
	
	/**
	 * 获取入仓订单列表
	 * @param unknown $field
	 * @param string $cond
	 * @param string $orderRaw
	 * @param number $limitStart
	 * @param number $limitPageSize
	 */
	public function getOrderList($field,$cond="",$orderRaw="",$limitStart=0,$limitPageSize=20) {
		if(empty($field)){
	        $field = "*";
	    }
	    if(!empty($orderRaw)){
	        $orderRaw = 'order by '.$orderRaw;
	    }
	    
	    $sql = "select {$field} from ".MICROSTOCKORDER." where 1 {$cond} {$orderRaw} limit {$limitStart},{$limitPageSize}";
	    $this->db->query($sql);
	    $result = $this->db->getRows();
	    
	    return $result;
	}
	
	/**
	 * 获取入仓订单列表总数
	 * @param string $cond
	 */
	public function getOrderListTotalNum($cond="") {
	    $total = 0;
	    $sql = "select count(*) as total from ".MICROSTOCKORDER." where 1 {$cond} ";
	    $this->db->query($sql);
	    $total = $this->db->fetchField('total');
	
	    return $total;
	}
	
	/**
	 * 取消微仓订单
	 * @param unknown $orderId 订单ID
	 * @param unknown $remark 备注
	 */
	public function cancelMicroOrder($orderId,$remark=""){
	    //查询用户ID
	    $sql = "select member_id,`status` from ".MICROSTOCKORDER." where order_id='{$orderId}'";
	    $this->db->query($sql);
	    $orderInfo = $this->db->fetchRow();
	    $member_id = $orderInfo['member_id'];
	    $status = $orderInfo['status'];
	    if($member_id){
	        $sql="update ".MICROSTOCKORDER." set admin_remark='$remark',status='0' where order_id='".$orderId."'";
	        $this->db->query($sql);
	        /*******取消后把库存加回去**********/
	        //查询订单商品
	        $sql = "select sku_id,num from ".MICROSTOCKORDERPRO." where order_id={$orderId}";
	        $this->db->query($sql);
	        $products = $this->db->getRows();
	        if(!empty($products)){
	            foreach ($products as $v)
	            {
	                $this->updateMicroShopStocks($member_id, $v['sku_id'],$v['num'],$status);
	            }
	        }
	        return true;
	    }
	    return false;
	}
	
	/**
	 * 更新微仓商品库存
	 * @param unknown $memberId
	 * @param unknown $skuId
	 * @param unknown $num
	 * @param unknown $status
	 */
	private function updateMicroShopStocks($memberId,$skuId,$num=0,$status){
	    $sql = "select id from ".MICROSTOCK." where member_id={$memberId} and sku_id={$skuId}";
	    $this->db->query($sql);
	    $id = $this->db->fetchField('id');
	    $num = intval($num);
	    
        //加回前台库存
        $this->db->query("update ".SETMEAL." set stock = stock+{$num},sell_amount=sell_amount-{$num} where id={$skuId}");
        
	    if($status>=2 && $id>0 && $num>0){//取消前是已经支付的才减微仓商品数量
	        //减微仓库存
	        $sql = "update ".MICROSTOCK." set num=num-{$num} where id={$id}";
	        $this->db->query($sql);
	        return true;
	    }
	    return false;
	}
	
	/**
	 * 更新订单状态
	 * @param unknown $orderId
	 * @param integer $status
	 * @param array   $arr
	 */
	public function updateOderStatus($orderId,$status,$arr=array())
	{
	    switch ($status)
	    {
	        case 2://付款
	            $sql = "update ".MICROSTOCKORDER." set payment_id={$arr['payment_id']},payment_name='{$arr['payment_name']}',admin_remark='{$arr['admin_remark']}' where order_id={$orderId}";
	            $this->db->query($sql);
	        default:
	            return false;
	            break;
	    }
	     
	    return true;
	}
	
	/**
	 * 获取销售额
	 * @param string $cond
	 * @param number $timeType 1为今天数据，2为昨天数据，3为本月数据
	 */
	public function getSalesInfo($cond="",$timeType=3) {
	    if(!empty($cond)){
	        $str = $cond;
	    }else{
	        $str = " and status>1";
	        switch ($timeType)
	        {
	            case 1://今天
	                $time = date('Y-m-d');
	                $str .= " and order_time>='{$time}'";
	                break;
	            case 2://昨天
	                $time = date('Y-m-d',strtotime('-1 day'));
	                $rtime = $time." 23:59:59";
	                $str .= " and order_time>='{$time}' and order_time<='{$rtime}'";
	                break;
	            case 3://本月
	                $time = date('Y-m');
	                $str .= " and order_time>='{$time}'";
	                break;
	            default://全部
	                break;
	        }
	    }
	    
	    $sql = "select sum(total_price) as total from ".MICROSTOCKORDER." where 1 {$str} and hp_order_id<=0 ";
	    $this->db->query($sql);
	    $total = $this->db->fetchField('total');
	     
	    return $total?$total:0;
	}
	
	/**
	 * 获取销售订单数量
	 * @param number $shopId 仓库ID
	 * @param number $timeType 1为今天数据，2为昨天数据，3为本月数据
	 */
	public function getOrderNum($shopId=0,$timeType=3) {
	    $str = "status>1";
	    if($shopId>0){
	        $str .= " and stock_shop={$shopId} ";
	    }
	    switch ($timeType)
	    {
	        case 1://今天
	            $time = date('Y-m-d');
	            $str .= " and order_time>='{$time}'";
	            break;
	        case 2://昨天
	            $time = date('Y-m-d',strtotime('-1 day'));
                $rtime = $time." 23:59:59";
	            $str .= " and order_time>='{$time}' and order_time<='{$rtime}'";
	            break;
	        case 3://本月
	            $time = date('Y-m');
	            $str .= " and order_time>='{$time}'";
	            break;
	        default://全部
	            break;
	    }
	    
	    $sql="select count(*) as total from ".MICROSTOCKORDER." where {$str} and hp_order_id<=0 ";
	    $this->db->query($sql);
	    $total = $this->db->fetchField('total');
	     
	    return $total?$total:0;
	}
}
?>